"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库time数据类型
Case Name   : 临时表使用time类型
Create At   : 2023.08.17
Owner       : z@wan005
Description :
    1、建本地临时表并插入数据
    2、创建全局临时表
    3、插入数据并查询
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest37(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0037"
        self.tb_name = "tb_time_0037"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '-step1:建本地临时表并插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name};
        create temporary table {self.tb_name}
        (c1 time) on commit preserve rows;
        insert {self.tb_name} values ('-838:59:59');
        insert {self.tb_name} values ('838:59:59');
        insert {self.tb_name} values ('837:59:59.9999999');
        insert {self.tb_name} values ('00:00:00');
        insert {self.tb_name} values ('0');
        insert {self.tb_name} values('24:59:59');
        insert {self.tb_name} values('');
        select * from {self.tb_name};'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.assertEqual(7, sql_res.count('INSERT 0 1'), '执行失败' + text)
        res_list = ['-838:59:59', '838:59:59', '838:00:00', '00:00:00',
                    '00:00:00', '24:59:59']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

        text = '-step2:创建全局临时表;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tb_name}_01;" \
                  f"create global temporary table " \
                  f"{self.tb_name}_01(c1 time) on commit preserve rows;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-step3:插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f"insert {self.tb_name}_01 values('-838:59:59');" \
                  f"insert {self.tb_name}_01 values('838:59:59');" \
                  f"insert {self.tb_name}_01 values('837:59:59.9999999');" \
                  f"insert {self.tb_name}_01 values('00:00:00');" \
                  f"insert {self.tb_name}_01 values('0');" \
                  f"insert {self.tb_name}_01 values('24:59:59');" \
                  f"insert {self.tb_name}_01 values('');" \
                  f"select * from {self.tb_name}_01;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(7, sql_res.count('INSERT 0 1'), '执行失败' + text)
        res_list = ['-838:59:59', '838:59:59', '838:00:00', '00:00:00',
                    '00:00:00', '24:59:59', '00:00:00']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step4:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
